package autokeyboard.gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;

import autokeyboard.data.PicturePanel;
import autokeyboard.handler.AboutWindowEvent;

@SuppressWarnings("serial")
public class AboutWindow extends JWindow
{
	private JButton	btClose	= new JButton("Close Window");
	private JDialog	dlg;

	public AboutWindow(JDialog dlg)
	{
		super(dlg);
		this.dlg = dlg;
		initComponent();

		this.setLocationRelativeTo(null);
	}

	private JPanel				pnPicPreview	= new JPanel();
	private JPanel				pnAuthorInfo	= new JPanel();
	private JPanel				pnSoftInfo		= new JPanel();

	private JLabel				lblAuthor_Name	= new JLabel();
	private JLabel				lblAuthor_Job	= new JLabel();
	private JLabel				lblAuthor_Email	= new JLabel();

	private JLabel				lblSoft_Name	= new JLabel();
	private JLabel				lblSoft_Version	= new JLabel();
	private JLabel				lblSoft_Pub		= new JLabel();

	private PanelPicture		pic				= new PanelPicture();
	private PicturePanel		background		= new PicturePanel();

	private AboutWindowEvent	event			= new AboutWindowEvent(lblAuthor_Name, lblAuthor_Job, lblAuthor_Email);

	private void initComponent()
	{
		setLblProperties();
		setMainLayout();
		setLayoutPnAuthor();
		setLayoutPnGame();
		setLayoutPnPicPreview();
		setWindowProperties();

		buttonEvent();
		windowEvent();
		event.start();
	}

	private void setLayoutPnAuthor()
	{
		pnAuthorInfo.setLayout(null);
		pnAuthorInfo.setOpaque(false);
		pnAuthorInfo.add(lblAuthor_Name);
		pnAuthorInfo.add(lblAuthor_Job);
		pnAuthorInfo.add(lblAuthor_Email);

		lblAuthor_Name.setBounds(10, 10, pnAuthorInfo.getWidth(), 40);
		lblAuthor_Job.setBounds(10, 50, pnAuthorInfo.getWidth(), 40);
		lblAuthor_Email.setBounds(10, 90, pnAuthorInfo.getWidth(), 40);
	}

	private void setLayoutPnGame()
	{
		pnSoftInfo.setLayout(null);
		pnSoftInfo.setOpaque(false);
//		pnSoftInfo.setBackground(new Color(201, 228, 255));
		pnSoftInfo.add(lblSoft_Name);
		pnSoftInfo.add(lblSoft_Version);
		pnSoftInfo.add(lblSoft_Pub);

		lblSoft_Name.setBounds(10, 10, pnSoftInfo.getWidth(), 40);
		lblSoft_Version.setBounds(10, 50, pnSoftInfo.getWidth(), 40);
		lblSoft_Pub.setBounds(10, 90, pnSoftInfo.getWidth(), 40);
	}

	private void setLayoutPnPicPreview()
	{
		pnPicPreview.setLayout(null);
		pnPicPreview.setOpaque(false);
		pnPicPreview.add(pic);
		pic.setBounds(1, 1, 148, 348);
		pic.folder = getClass().getResource("/Normal_Icons/Avatar.png");
		pic.repaint();
	}

	private void setWindowProperties()
	{
		this.setSize(525, 370);
		this.setLocationRelativeTo(null);
		this.setAlwaysOnTop(true);
		this.setOpacity(0.9f);
	}

	private void setMainLayout()
	{
		this.add(background);
		background.path = "/Background/About.png";
		background.repaint();
		background.setLayout(null);
		background.add(pnPicPreview);
		background.add(pnAuthorInfo);
		background.add(pnSoftInfo);
		background.add(btClose);

		pnPicPreview.setBorder(BorderFactory.createTitledBorder(""));
		pnAuthorInfo.setBorder(BorderFactory.createTitledBorder(""));
		pnSoftInfo.setBorder(BorderFactory.createTitledBorder(""));

		pnPicPreview.setBounds(10, 10, 150, 350);
		pnAuthorInfo.setBounds(pnPicPreview.getX() + pnPicPreview.getWidth() + 5, pnPicPreview.getY(), 350, 150);
		pnSoftInfo.setBounds(pnAuthorInfo.getX(), pnAuthorInfo.getY() + pnAuthorInfo.getHeight() + 5, pnAuthorInfo.getWidth(), 150);
		btClose.setBounds(pnSoftInfo.getX(), pnSoftInfo.getY() + pnSoftInfo.getHeight() + 5, pnSoftInfo.getWidth(), 40);
	}

	private void setLblProperties()
	{
		lblAuthor_Name.setFont(new Font("Comic Sans MS", 1, 18));
		lblAuthor_Name.setForeground(Color.white);
		
		lblAuthor_Job.setFont(lblAuthor_Name.getFont());
		lblAuthor_Job.setForeground(lblAuthor_Name.getForeground());
		
		lblAuthor_Email.setFont(lblAuthor_Name.getFont());
		lblAuthor_Email.setForeground(lblAuthor_Name.getForeground());

		lblSoft_Name.setFont(new Font("Comic Sans MS", 1, 18));
		lblSoft_Version.setFont(lblSoft_Name.getFont());
		lblSoft_Pub.setFont(lblSoft_Name.getFont());

		lblSoft_Name.setText("Name: Auto Keyboard");
		lblSoft_Name.setForeground(lblAuthor_Name.getForeground());
		
		lblSoft_Version.setText("Version 1.0");
		lblSoft_Version.setForeground(lblAuthor_Name.getForeground());
		
		lblSoft_Pub.setText("Published in 2013");
		lblSoft_Pub.setForeground(lblAuthor_Name.getForeground());
	}

	private void buttonEvent()
	{
		btClose.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e)
			{
				event.stop();
				dispose();
				dlg.dispose();
			}
		});

		btClose.addKeyListener(new KeyAdapter()
		{
			@Override
			public void keyPressed(KeyEvent evt)
			{
				event.stop();
				dispose();
				dlg.dispose();
			}
		});
	}

	private void windowEvent()
	{
		this.addWindowListener(new WindowAdapter()
		{
			@Override
			public void windowClosed(WindowEvent evt)
			{
				event.stop();
			}
		});
	}
}
